/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.yihu.wlyy.service.app.team;

import com.yihu.wlyy.entity.doctor.Doctor;
import com.yihu.wlyy.entity.doctor.DoctorPatient;
import com.yihu.wlyy.entity.doctor.DoctorTeam;
import com.yihu.wlyy.repository.DoctorDao;
import com.yihu.wlyy.repository.DoctorPatientDao;
import com.yihu.wlyy.repository.DoctorTeamDao;
import com.yihu.wlyy.repository.DrHealthTeamMemberDao;
import com.yihu.wlyy.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 医生健康治疗团队、工作组业务处理类.
 *
 * @author George
 */
@Component
@Transactional
public class DrHealthTeamService extends BaseService {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private DoctorPatientDao doctorPatientDao;
    @Autowired
    private DoctorTeamDao doctorTeamDao;
    @Autowired
    private DrHealthTeamMemberDao doctorTeamMemberDao;
    @Autowired
    private DoctorDao doctorDao;

    /**
     * 查询签约团队的其他医生
     *
     * @param patient  患者标识
     * @param doctor   需要排队的医生
     * @param teamType 团队类型：1三师，2家庭
     */
    @SuppressWarnings("unused")
    public Iterable<DoctorPatient> findTeamOtherMembers(String patient, String doctor, int teamType) {
        return doctorPatientDao.findTeamOtherDoctor(patient, doctor, teamType);
    }

    /**
     * 查询团队的医生信息
     *
     * @param patient  患者标识
     * @param teamType 团队类型：1三师，2家庭
     * @return
     */
    public List<Doctor> findTeamDoctors(String patient, int teamType) {
        return doctorPatientDao.findDoctors(patient, teamType);
    }

    /**
     * 查询团队医生
     *
     * @param team
     * @return
     */
    public List<Doctor> findTeamDoctors(String team) {
        return doctorPatientDao.findTeamDoctors(team);
    }

    /**
     * 查询某个医生的团队健康管理师或全科医生
     *
     * @param doctor
     * @param type
     * @return
     */
    public List<Object> findTeamDoctors(String doctor, String type, Integer page, Integer pageSize) {
        if (pageSize == null || pageSize <= 0) {
            pageSize = 10;
        }
        if (page == null || page <= 0) {
            page = 1;
        }
        List<Object> doctors = new ArrayList<Object>();
        int end = page * pageSize - 1;
        int start = (page - 1) * pageSize;
        if (type.equals("2")) {
            //2是全科医生
            //健康管理师列表
            String sql = "select DISTINCT wd.code code, wd.name name,wd.photo photo ," +
                    " ( select count(*) from wlyy_sign_family wsf where wsf.doctor_health=wd.code and wsf.status=1 and wsf.type = 2) count, wd.sex from " +
                    " wlyy_doctor_team_member w," +
                    " (select w1.team team from wlyy_doctor_team_member w1 where w1.member_code= ? and w1.del=1) tems , " +
                    " wlyy_doctor wd  where w.team=tems.team and wd.code=w.member_code and w.type=3 and w.del=1 limit ?,? ";
            List<Map<String, Object>> doctor3s = jdbcTemplate.queryForList(sql, doctor, start, pageSize);
            if (doctor3s == null) {
                doctor3s = new ArrayList<Map<String, Object>>();
            }
            doctors.add(doctor3s);
            //专业医生列表
            sql = "select DISTINCT wd.code code," +
                    " wd.name name," +
                    " wd.photo photo," +
                    " wd.hospital_name hospitalName," +
                    " wd.dept_name deptName, " +
                    "wd.sex sex ," +
                    " (select count(*) from wlyy_consult_team_doctor where to_doctor=w.member_code and date(czrq) = curdate()) currentCount ," +
                    " (select count(*) from wlyy_consult_team_doctor where to_doctor=w.member_code ) allCount " +
                    "from " +
                    " wlyy_doctor_team_member w," +
                    " (select w1.team team from wlyy_doctor_team_member w1 where w1.member_code=? and w1.del=1 ) tems ," +
                    " wlyy_doctor wd  where w.team=tems.team and wd.code=w.member_code and w.type=1 and w.del=1 limit ?,?";
            List<Map<String, Object>> doctor1s = jdbcTemplate.queryForList(sql, doctor, start, pageSize);
            if (doctor1s == null) {
                doctor1s = new ArrayList<Map<String, Object>>();
            }
            doctors.add(doctor1s);
            //doctors = signFamilyDao.findTeamQkDoctors(doctor);
        } else if (type.equals("3")) {
            //3是健康管理师

            //全科医生列表
            String sql = "select DISTINCT " +
                    " wd.code code," +
                    " wd.name name," +
                    " wd.photo photo," +
                    " wd.hospital_name hospitalName, wd.sex from wlyy_doctor_team_member w," +
                    "(select w1.team team from wlyy_doctor_team_member w1 where w1.member_code=? and w1.del=1) tems ," +
                    " wlyy_doctor wd  where w.team=tems.team and wd.code=w.member_code and w.type=2 and w.del=1 limit ?,? ";
            List<Map<String, Object>> doctor3s = jdbcTemplate.queryForList(sql, doctor, start, pageSize);
            if (doctor3s == null) {
                doctor3s = new ArrayList<Map<String, Object>>();
            }
            doctors.add(doctor3s);
            //专业医生列表
            sql = "select DISTINCT wd.code code," +
                    " wd.name name," +
                    " wd.photo photo," +
                    " wd.hospital_name hospitalName," +
                    " wd.dept_name deptName, wd.sex sex, " +
                    " (select count(*) from wlyy_consult_team_doctor where to_doctor=w.member_code and date(czrq) = curdate()) currentCount ," +
                    " (select count(*) from wlyy_consult_team_doctor where to_doctor=w.member_code ) allCount " +
                    "from " +
                    " wlyy_doctor_team_member w," +
                    " (select w1.team team from wlyy_doctor_team_member w1 where w1.member_code=?  and w1.del=1) tems ," +
                    " wlyy_doctor wd  where w.team=tems.team and wd.code=w.member_code and w.type=1 and w.del=1 limit ?,?";

            List<Map<String, Object>> doctor2s = jdbcTemplate.queryForList(sql, doctor, start, pageSize);
            if (doctor2s == null) {
                doctor2s = new ArrayList<Map<String, Object>>();
            }
            doctors.add(doctor2s);
        }

        return doctors;
    }

    public Integer teamListCountByDoctor(String doctorCode) {

        return doctorTeamMemberDao.teamListByDoctorCode(doctorCode);
    }

    public Integer teamListByDoctorCode(String doctorCode) {

        return doctorTeamMemberDao.teamListByDoctorCode(doctorCode);
    }

    /**
     * 查询某个医生的团队健康管理师或全科医生
     *
     * @param doctor
     * @param type
     * @return
     */
    public List<Object> findTeamDoctorsByType(String doctor, String type, Integer page, Integer pageSize) {
        if (pageSize == null || pageSize <= 0) {
            pageSize = 10;
        }
        if (page == null || page <= 0) {
            page = 1;
        }
        List<Object> doctors = new ArrayList<Object>();
        int end = page * pageSize - 1;
        int start = (page - 1) * pageSize;
        if (type.equals("1")) {
            //专业医生列表
            String sql = "select DISTINCT wd.code code," +
                    " wd.name name," +
                    " wd.photo photo," +
                    " wd.hospital_name hospitalName," +
                    " wd.dept_name deptName, " +
                    " wd.sex sex ," +
                    " from " +
                    " wlyy_doctor_team_member w," +
                    " (select w1.team team from wlyy_doctor_team_member w1 where w1.member_code='" + doctor + "' and w1.del=1 ) tems ," +
                    " wlyy_doctor wd  where w.team=tems.team and wd.code=w.member_code and w.type=1 and w.del=1 limit  " + start + "," + end;

            List<Map<String, Object>> doctor1s = jdbcTemplate.queryForList(sql);
            doctors.add(doctor1s);
        } else if (type.equals("2")) {
            //全科医生列表
            String sql = "select " +
                    " DISTINCT wd.code code," +
                    " wd.name name," +
                    " wd.photo photo," +
                    " wd.hospital_name hospitalName from wlyy_doctor_team_member w," +
                    "(select w.team team from wlyy_doctor_team_member w where w.member_code='" + doctor + "' and w1.del=1) tems ," +
                    " wlyy_doctor wd  where w.team=tems.team and wd.code=w.member_code and w.type=2 and w.del=1 limit " + start + "," + end;
            List<Map<String, Object>> doctor3s = jdbcTemplate.queryForList(sql);
            doctors.add(doctor3s);
        } else if (type.equals("3")) {
            //健康管理师列表
            String sql = "select " +
                    " DISTINCT wd.code code, " +
                    " wd.name name," +
                    " wd.photo photo ," +
                    " ( select count(*) from wlyy_sign_family wsf where wsf.doctor_health='" + doctor + "' ) count from " +
                    " wlyy_doctor_team_member w," +
                    " (select w.team team from wlyy_doctor_team_member w where w.member_code='" + doctor + "' and w1.del=1) tems , " +
                    " wlyy_doctor wd  where w.team=tems.team and wd.code=w.member_code and w.type=3 and w.del=1 limit " + start + "," + end;
            List<Map<String, Object>> doctor3s = jdbcTemplate.queryForList(sql);
            doctors.add(doctor3s);
        }

        return doctors;
    }

    public List<Doctor> findSanshiTeamDoctors(String parientCode) {
        //查看当前的患者所在的三师团队
        DoctorTeam doctorTeam = doctorTeamDao.findBySanshiParientCode(parientCode);
        //查看三师团队里面的医生医生
        if (doctorTeam != null) {
            List<Doctor> doctors = doctorDao.findSanshiDoctorByTeam(doctorTeam.getCode());
            return doctors;
        } else {
            return null;
        }
    }

    public List<Doctor> findJiatingTeamDoctors(String parientCode) {
        //查看当前的患者所在的家庭团队
        DoctorTeam doctorTeam = doctorTeamDao.findByParientCode(parientCode);
        //查看家庭团队里面的医生
        if (doctorTeam != null) {
            List<Doctor> doctors = doctorDao.findJiatingDoctorByTeam(doctorTeam.getCode());
            return doctors;
        } else {
            return null;
        }
    }
}
